package com.csk.zsk.compents.action;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csk.zsk.commons.action.AjaxActionSupport;
import com.csk.zsk.commons.utils.encode.JsonBinder;
import com.csk.zsk.commons.utils.web.struts2.Struts2Utils;
import com.csk.zsk.compents.entity.Video;
import com.csk.zsk.compents.entity.VideoItem;
import com.csk.zsk.compents.service.VideoItemService;
import com.csk.zsk.compents.service.VideoService;
import com.csk.zsk.compents.utils.URLUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@Scope("prototype")
public class VideoAction extends AjaxActionSupport {

	private static final long serialVersionUID = 2175847091406999231L;
	private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();

	@Autowired
	private VideoService videoService;
	@Autowired
	private VideoItemService videoItemService;

	private Integer num;
	private String type;
	private String year;
	private String area;
	private String videoLikeName;
	private String id;
	private String videoItemId;

	public static final String MOVIE_DAILY = "http://api.tv.sohu.com/movie/top50/views/daily.json?api_key=8c2050e8f750b360df2d69d72f39a939"; // 搜狐电影排行榜 播放日榜
	public static final String TELEPLAY_DAILY = "http://api.tv.sohu.com/teleplay/top50/views/daily.json?api_key=8c2050e8f750b360df2d69d72f39a939"; // 电视剧排行榜 播放日榜
	public static final String ANIMATION_DAILY = "http://api.tv.sohu.com/animation/top50/views/daily.json?api_key=8c2050e8f750b360df2d69d72f39a939"; // 动漫排行榜 播放日榜
	public static final String ZONGYI_DAILY = "http://api.tv.sohu.com/zongyi/top50/views/daily.json?api_key=8c2050e8f750b360df2d69d72f39a939"; // 综艺排行榜 播放日榜
	public static final String MUSIC_DAILY = "http://api.tv.sohu.com/music/top50/views/daily.json?api_key=8c2050e8f750b360df2d69d72f39a939"; // 音乐排行榜 播放日榜
	public static final String DOCUMMENTARY_DAILY = "http://api.tv.sohu.com/documentary/top50/views/daily.json?api_key=8c2050e8f750b360df2d69d72f39a939"; // 纪录片排行榜 播放日榜
	public static final String EDUCATION_DAILY = "http://api.tv.sohu.com/education/top50/views/daily.json?api_key=8c2050e8f750b360df2d69d72f39a939"; // 教育片排行榜 播放日榜

	private Map<String, String> map = null;
	private Map<Integer, String> typeMap = null;

	public VideoAction() {
		initMap();
	}

	public void importVideo() {
		String s = Struts2Utils.getParameter("s");
		String url = map.get(s);
		String str = URLUtils.readContentFromUrl(url, "UTF-8");
		JsonObject1 jsonObject = binder.fromJson(str, JsonObject1.class);
		List<Video> videos = jsonObject.getData().getVideos();
		for (int i = 0; i < videos.size(); i++) {
			Video video = videos.get(i);
			String sid = video.getSid();
			Integer pageCount = 1;
			Integer total = video.getTv_set_now();
			List<VideoItem> list = Lists.newArrayList();
			if (total != null && total.intValue() != 0) {
				pageCount = (video.getTv_set_now() - 1) / 50 + 1;
			}
			try {
				for (int j = 1; j <= pageCount; j++) {
					String SET_LIST = "http://api.tv.sohu.com/set/list/{0}.json?page={1}&pageSize=50&api_key=8c2050e8f750b360df2d69d72f39a939"; // 分集列表，包括每一集的截图，剧情，播放地址等信息
					SET_LIST = SET_LIST.replace("{0}", sid);
					SET_LIST = SET_LIST.replace("{1}", j + "");
					String itemStr = URLUtils.readContentFromUrl(SET_LIST, "UTF-8");
					JsonObject2 itemJsonObject = binder.fromJson(itemStr, JsonObject2.class);
					list.addAll(itemJsonObject.getData().getVideos());
				}
				video.setVideoItemList(list);
				video.setType(s);

				videoService.saveVideo(video);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void initMap() {
		map = Maps.newHashMap();
		map.put("1", MOVIE_DAILY);
		map.put("2", TELEPLAY_DAILY);
		map.put("3", ANIMATION_DAILY);
		map.put("4", ZONGYI_DAILY);
		map.put("5", MUSIC_DAILY);
		map.put("6", DOCUMMENTARY_DAILY);
		map.put("7", EDUCATION_DAILY);

		typeMap = Maps.newHashMap();
		typeMap.put(1, "全部");
		typeMap.put(2, "电影");
		typeMap.put(3, "电视剧");
		typeMap.put(4, "动漫");
		typeMap.put(5, "综艺");
		typeMap.put(6, "音乐");
		typeMap.put(7, "纪录片");
		typeMap.put(8, "教育片");
	}

	public void getHotVideo() throws Exception {
		List<Video> videoList = videoService.getVideoByType(type, num);
		String json = binder.toJson(videoList);
		Struts2Utils.renderJson(json);
	}

	public void getVideoByType() throws Exception {
		List<Video> videoList = videoService.getVideoByType(type, num);
		String json = binder.toJson(videoList);
		Struts2Utils.renderJson(json);
	}

	public void getVideoByYearAndArea() throws Exception {
		List<Video> videoList = null;
		if (year.trim().equals("全部") && area.trim().equals("全部")) {
			videoList = videoService.getVideoByType(type, num);
		}
		if (year.trim().equals("全部") && !area.trim().equals("全部")) {
			videoList = videoService.getVideoByArea(area, type);
		}
		if (!year.trim().equals("全部") && !year.trim().equals("更早") && area.trim().equals("全部")) {
			videoList = videoService.getVideoByYear(Integer.parseInt(year), type);
		}
		if (year.trim().equals("更早") && !area.trim().equals("全部")) {
			videoList = videoService.getVideoByLessThanYearAndArea(2003, area, type);
		}
		if (year.trim().equals("更早") && area.trim().equals("全部")) {
			videoList = videoService.getVideoByLessThanYear(2003, type);
		}
		if (!year.trim().equals("全部") && !year.trim().equals("更早") && !area.trim().equals("全部")) {
			videoList = videoService.getVideoByYearAndArea(Integer.parseInt(year), area, type);
		}
		String json = binder.toJson(videoList);
		Struts2Utils.renderJson(json);
	}

	public void getVideoByLikeName() throws Exception {
		List<JsonObject3> list = Lists.newArrayList();
		List<Video> videoList = Lists.newArrayList();
		Integer lastType = 0;
		if (videoLikeName == null || videoLikeName.equals("")) {
			return;
		}
		if (type == null || type.equals("")) {
			videoList = videoService.searchVideoByTVName(videoLikeName);
		} else {
			videoList = videoService.searchVideoByTVNameAndType(videoLikeName, type);
		}

		if (videoList != null && videoList.size() > 0) {
			JsonObject3 jsonObject = new JsonObject3();
			jsonObject.setType(1);
			jsonObject.setTypeName("全部");
			jsonObject.setVideos(videoList);
			list.add(jsonObject);
			JsonObject3 jsonObject3 = null;
			List<Video> l = null;
			for (int i = 0; i < videoList.size(); i++) {
				Video v = videoList.get(i);
				if ((Integer.parseInt(v.getType()) + 1) != lastType.intValue()) {
					if (jsonObject3 != null) {
						list.add(jsonObject3);
					}
					jsonObject3 = new JsonObject3();
					jsonObject3.setType(Integer.parseInt(v.getType()) + 1);
					jsonObject3.setTypeName(typeMap.get(Integer.parseInt(v.getType()) + 1));
					l = Lists.newArrayList();
					l.add(v);
					jsonObject3.setVideos(l);
				} else {
					jsonObject3.getVideos().add(v);

				}
				if (i == videoList.size() - 1) {
					list.add(jsonObject3);
				}
				lastType = Integer.parseInt(v.getType()) + 1;
			}
		}
		String json = binder.toJson(list);
		Struts2Utils.renderJson(json);
	}

	public void getVideoById() throws Exception {
		Video video = videoService.getVideoById(id);
		String json = binder.toJson(video);
		Struts2Utils.renderJson(json);
	}

	public void getVideoItemListById() throws Exception {
		Video video = videoService.getVideoById(id);
		List<VideoItem> videoItemList = video.getVideoItemList();
		String json = binder.toJson(videoItemList);
		Struts2Utils.renderJson(json);
	}

	public void getVideoItemByItemId() throws Exception {
		VideoItem videoItem = videoItemService.getVideoItemById(videoItemId);
		String json = binder.toJson(videoItem);
		Struts2Utils.renderJson(json);
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public void setVideoLikeName(String videoLikeName) {
		this.videoLikeName = videoLikeName;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setVideoItemId(String videoItemId) {
		this.videoItemId = videoItemId;
	}

	public static class JsonObject1 {
		String status;
		String statusText;
		Data1 data;

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		public String getStatusText() {
			return statusText;
		}

		public void setStatusText(String statusText) {
			this.statusText = statusText;
		}

		public Data1 getData() {
			return data;
		}

		public void setData(Data1 data) {
			this.data = data;
		}

	}

	public static class JsonObject2 {
		String status;
		String statusText;
		Data2 data;

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		public String getStatusText() {
			return statusText;
		}

		public void setStatusText(String statusText) {
			this.statusText = statusText;
		}

		public Data2 getData() {
			return data;
		}

		public void setData(Data2 data) {
			this.data = data;
		}

	}

	public static class Data1 {
		List<Video> videos;

		public List<Video> getVideos() {
			return videos;
		}

		public void setVideos(List<Video> videos) {
			this.videos = videos;
		}

	}

	public static class Data2 {
		List<VideoItem> videos;

		public List<VideoItem> getVideos() {
			return videos;
		}

		public void setVideos(List<VideoItem> videos) {
			this.videos = videos;
		}

	}

	public static class JsonObject3 {
		private Integer type;
		private String typeName;
		private List<Video> videos;

		public Integer getType() {
			return type;
		}

		public void setType(Integer type) {
			this.type = type;
		}

		public String getTypeName() {
			return typeName;
		}

		public void setTypeName(String typeName) {
			this.typeName = typeName;
		}

		public List<Video> getVideos() {
			return videos;
		}

		public void setVideos(List<Video> videos) {
			this.videos = videos;
		}

	}
}
